
$(document).ready(function () {
		$('#boutStorage_datebox').datetimebox('setValue', formatterDate(new Date()));	
	
		setType();
		loadtab();
	})	

//得到当前日期
	formatterDate = function (date) {
		var day = date.getDate() > 9 ? date.getDate() : "0" + date.getDate();
		var month = (date.getMonth() + 1) > 9 ? (date.getMonth() + 1) : "0"
		+ (date.getMonth() + 1);
		var hor = date.getHours();
		var min = date.getMinutes();
		var sec = date.getSeconds();
		return date.getFullYear() + '-' + month + '-' + day+" "+hor+":"+min+":"+sec;
		};
	function  loadtab()
	{	
//		var url=cxt+"/boutStorage/getPersion";
//		
//		var  persionurl='/boutStorage/getPersion';
//		var  persionData = common_ajax.ajaxFunc(persionurl,null,"json");
		var columns=[[
		              {field:'proids',title:'产品ids',hidden:'true'},
		              {field:'proname',title:'产品名称',width:100,align:'center'},
		              {field:'typeids',title:'类别ids',hidden:'true'},
		              {field:'productclass',title:'产品类别',width:100,align:'center'},
		              {field:'stocknow',title:'当前库存',width:100,align:'center'},
		              {field:'boutstoragenumber',title:'出库数量',width:100,align:'center',
		            	  editor:{type:'numberspinner',
								min:1,
								max:100,
								options:{
								 onChange : function(n){
									 sum(n);
								}
							}
						
						}},
		              {field:'total',title:'剩余库存',width:100,align:'center'},
		              {field:'reasonids',title:'出库原因ids',hidden:'true'},
		              {field:'reason',title:'出库原因',width:100,align:'center'},
		              {field:'bz',title:'备注',width:100,align:'center',editor:'textbox'},
		              {field:'operate',title:'操作',width:100,align:'center',formatter:go}
		              ]];
		
		$('#outStorage').datagrid({
			  iconCls: 'icon-edit',
			  singleSelect:true,  
			  fitColumns:true,
			  onClickRow:onClickRow,
			  columns:columns
		});
		
		
	}

//获取产品类别
			function  setType()
			{
					var com=$('#selType1_combobox');
					com.combobox({
							url: 'buyProduct/selProCode',
//						  data:daJson,
						  valueField:'ids', 
						  textField:'name',
//						  filter: function(q, row){ 
//						        var opts = $(this).combobox('options'); 
//						       // .toUpperCase()小写转大写   toLowerCase()大写转小写
//						        return row[opts.textField].toLowerCase().indexOf(q.toLowerCase()) >= 0;//同一转换成小写做比较，这里改成>=即可在任意地方匹配  
//						   },
//						  formatter: function(row)
//						  {		
//							  var opts = $(this).combobox('options');
//							  row[opts.textField]=row.typebh+"("+row.name+")";
//							  return    row[opts.textField];
//						  },
						  onSelect: function(row) 
							{	
			//				  var opts = $(this).combobox('options');
//								getProduct(row.ids);
								setCombobox(row.ids);
							} 
					});
					
		
			}
			//获取产品列表放入下拉
			function  setCombobox(typeIds)
			{	
				var url='/buyProduct/selProduct';
				var data={"typeIds":typeIds};
				var reData = common_ajax.ajaxFunc(url,data);
				var daJson=$.parseJSON(reData);
				var com=$('#addPro1_combobox');
				com.combobox({
					  data:daJson,
					  valueField:'ids', 
					  textField:'probh',
					  filter: function(q, row){ 
					        var opts = $(this).combobox('options'); 
					       // .toUpperCase()小写转大写   toLowerCase()大写转小写
					        return row[opts.textField].toLowerCase().indexOf(q.toLowerCase()) >= 0;//同一转换成小写做比较，这里改成>=即可在任意地方匹配  
					   },
					  formatter: function(row)
					  {		
						  var opts = $(this).combobox('options');
						  row[opts.textField]=row.probh+"("+row.name+")";
						  return    row[opts.textField];
					  },
					  onSelect: function(row) 
						{	
		//				  var opts = $(this).combobox('options');
							getProduct(row.ids);
						} 
				});
		//		
				
			}
			
			//选中产品编号后查出产品
			function getProduct(proIds)
			{		
				//获取所有
				var url="/bintoStorage/getProInfo";
				var data={"productIds":proIds};
				var reData = common_ajax.ajaxFunc(url, data,"json");
						if(reData.stocknow == "0")
						{
							$.alert(parent.$, "该产品库存为0，无法出库！");
						}else
						{	
							reData.boutstoragenumber='1';
							reData.total=parseFloat(reData.stocknow)-parseFloat(reData.boutstoragenumber);
							reData.reason=$('#ckyy_select').combobox('getText');
							reData.reasonids=$('#ckyy_select').combobox('getValue');
							$('#outStorage').datagrid('insertRow',{row:reData});
						}
			}
//			/*剩余库存*/
//			function total(n)
//			{	
//				var row = $('#outStorage').datagrid('getSelected');
//				var index=$('#outStorage').datagrid('getRowIndex', row);
//				var sum=parseInt(row.stocknow)-parseInt(n);
//				$('#outStorage').datagrid('updateRow', {
//			        index: index,
//			        row: {
//			        	intostoragenumber:n,
//			        	total:sum
//			        }
//			    });
//				$('#outStorage').datagrid('beginEdit', index);
//			}
			//计算库存
			function sum(n)
			{	
				var row = $('#outStorage').datagrid('getSelected');
				var index=$('#outStorage').datagrid('getRowIndex', row);
				var sum=parseFloat(row.stocknow)-n;
				$('#outStorage').datagrid('updateRow', {
	                index: index,
	                row: {
	                	boutstoragenumber:n,
	                	total:sum
	                }
	            });
				$('#outStorage').datagrid('beginEdit', index);
			}
			/*确认出库
			 * 
			 */
			function  saveBoutStorage()
			{
				$('#outStorage').datagrid("endEdit", editIndex1);
				 var rows = $('#outStorage').datagrid('getRows');
				 for(var i=0;i<rows.length;i++)
				 {
					 var rowIndex = $('#outStorage').datagrid('getRowIndex', rows[i]);
					 $('#outStorage').datagrid('endEdit', rowIndex);
					 var proids = rows[i].proids;
					 var boutstoragenumber = rows[i].boutstoragenumber;
					 var typeids=rows[i].typeids;
					 var reasonids=rows[i].reasonids;
					 var bz=rows[i].bz;
					$("#boutProduct").append("<input type='hidden' name='productOutinList["+i+"].productIds' value='"+proids+"'/>");
					$("#boutProduct").append("<input type='hidden' name='productOutinList["+i+"].typeIds' value='"+typeids+"'/>");
					$("#boutProduct").append("<input type='hidden' name='productOutinList["+i+"].number' value='"+boutstoragenumber+"'/>");
					$("#boutProduct").append("<input type='hidden' name='productOutinList["+i+"].reason' value='"+reasonids+"'/>");
					$("#boutProduct").append("<input type='hidden' name='productOutinList["+i+"].bz' value='"+bz+"'/>");
				 }
			 		if(rows.length>0)
			 		{
			 			   var url='/boutStorage/saveBoutStorage';
							var data=$('#boutStorage_form').serialize();
							var reData = common_ajax.ajaxFunc(url,data);
							if(reData == "200")
							{
								 $.slideMsg("保存成功", parent.$);
								 parent.refresh();
							}else
							{
								$.alert(parent.$, reData);
							}
						 
			 		}else
			 		{
			 			$.alert(parent.$, "未添加出库产品，请先添加");
			 		}
				  
				 
			}
			/*数据表格行编辑*/
			var editIndex1 = undefined;
			function endEditing(){
				if (editIndex1 == undefined){return true}
				if ($('#outStorage').datagrid('validateRow', editIndex1)){
					$('#outStorage').datagrid('endEdit', editIndex1);
						editIndex1 = undefined;
						return true;
				} else {
					return false;
				}
			}
			function onClickRow(index){
				if (editIndex1 != index){
					if (endEditing()){
						$('#outStorage').datagrid('selectRow', index)
							.datagrid('beginEdit', index);
							editIndex1 = index;
					} else {
						$('#outStorage').datagrid('selectRow', editIndex1);
					}
				}
			}
			/*数据表格行删除*/	
			function  go(val,row){
				var path = cxt + "/sb/themes/insdep/icons/edit_remove.png";
				return "<a href='#'  class='button-delete button-danger  l-btn l-btn-small' onclick='deleterow(this)' ><span class='l-btn-left' ><span class='l-btn-text'>删除</span></span></a>";
			} 
			function getRowIndex(target){
				var tr = $(target).closest('tr.datagrid-row');
				return parseInt(tr.attr('datagrid-row-index'));
			}
			function deleterow(target){
				$.messager.confirm('Confirm','确定删除？',function(r){
					if (r){
						$('#outStorage').datagrid('deleteRow', getRowIndex(target));
					}
				});
			}